package webServlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import cn.login.test.TransformationCode;
import cn.user.domain.Detialed;
import cn.user.domain.pageBean;
import cn.user.domain.dao.DetialDao;

@WebServlet("/searchServlet")
public class searchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String keyword = request.getParameter("keyword");
		keyword = TransformationCode.unescape(keyword);
		int currentpage = Integer.parseInt(request.getParameter("currentpage"));
		double count = 0;
		DetialDao dd = new DetialDao();
		count = dd.count(keyword);
		int pageCount = (int)Math.ceil(count/4);
		List list = new ArrayList<Detialed>();
		list = dd.findbySearch(keyword, currentpage);
		pageBean pb = new pageBean();
		pb.setCount((int)count);
		pb.setCurrentPage(currentpage);
		pb.setPageCount(pageCount);
		pb.setList(list);
		ObjectMapper mapper = new ObjectMapper();
        response.setContentType("application/json;charset=utf-8");
        mapper.writeValue(response.getOutputStream(),pb);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
